<?php
    include_once(SITE_ROOT.'/includes/db.inc.php');//Chứa mã kết nối mysql
    class BookDAL{
        
        //Lấy thông tin về tất cả các cuốn sách hiện có
        function get_all_book(){
            $sql = "select b.*,c.CateName from books as b, categories as c where b.CateId = c.CateId order by BookId DESC";
            $result = mysql_query($sql);
            $total_records = mysql_num_rows($result);

            $arr_return = array(
                'books' => array(),
                'total' => $total_records
            );
            
            while($row = mysql_fetch_assoc($result)){
                $arr_return['books'][] = $row;
            }
            return $arr_return;       
        }
       
        
        //Lấy thông tin về tất cả các cuốn sách đặc biệt
        function get_specials_list($arr_args =''){
            $sql = "select b.*,c.CateName from books as b, categories as c where b.CateId = c.CateId and SpecialGift = 1 order by BookId DESC";
            $result = mysql_query($sql);
            $total_records = mysql_num_rows($result);

            $arr_return = array(
                'books' => array(),
                'total' => $total_records
            );
            
            while($row = mysql_fetch_assoc($result)){
                $arr_return['books'][] = $row;
            }
                 
            return $arr_return;     
        }
        
        //Lấy thông tin về tất cả các cuốn sách tiêu biểu và 6 quyển sách mới
        function get_list_books_index($arr_args=''){
            //Get thông tin về tất cả các cuốn sách tiêu biểu
            $sql = "select b.*,c.CateName from books as b, categories as c where b.CateId = c.CateId and FeaturedBook = 1 order by BookId DESC";
            $result = mysql_query($sql);
            $total_records = mysql_num_rows($result);

            $arr_featured = array(
                'books' => array(),
                'total' => $total_records
            );
            
            while($row = mysql_fetch_assoc($result)){
                $arr_featured['books'][] = $row;
            }
            
            //Get thông tin về 6 cuốn sách mới
            $sql = "select b.*,c.CateName from books as b, categories as c where b.CateId = c.CateId order by BookId DESC limit 0,6";
            $result = mysql_query($sql);
            $total_records = mysql_num_rows($result);

            $arr_news = array(
                'books' => array(),
                'total' => $total_records
            );
            
            while($row = mysql_fetch_assoc($result)){
                $arr_news['books'][] = $row;
            }
            
            $arr_return = array(
                'featured' => $arr_featured,
                'news' => $arr_news
            );
            
            
            return $arr_return;     
        }
        
        //Lấy thông tin về một cuốn sách có id nằm trong mảng tham số truyền vào 
        function detail($arr_args =''){
            $book_id = $arr_args['book_id']; 
            $sql = "select b.*,c.CateName from books as b, categories as c where b.CateId = c.CateId and b.BookId = ".$book_id;
            $result = mysql_query($sql);
            $total_records = mysql_num_rows($result);
            
            $arr_return = array(
                'book' => array(),
                'books' => array(),
                'error_message' => ''
            );
            
            if($total_records >0){
                $book = mysql_fetch_assoc($result);
                $arr_return['book'] = $book;
                
                $sql1 ="select BookId,CateId,Name,ImageUrl from books where BookID <> ".$book['BookId'] ." and CateId = ".$book['CateId'];
                $result1 = mysql_query($sql1);
                if($result1!=false){
                    while($row1 = mysql_fetch_assoc($result1)){
                        $arr_return['books'][] =  $row1;
                    }
                }
            }else{
                $arr_return['error_message'] = "Xin vui lòng kiểm tra l?i du?ng d?n!";
            }
            
            return $arr_return;
        }
        
        function checkBookCode($bookcode)
        {
            $cmd = "select * from books where Code = '" . $bookcode . "'";
            $result = mysql_query($cmd);
            if(mysql_num_rows($result) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        
        function getAllBook()
        {
            $select_command = "select * from books,categories where books.CateId = categories.CateId";
            $result = mysql_query($select_command);
            
            if($result != false)
            {
                if(mysql_num_rows($result) > 0)
                {
                    while($row = mysql_fetch_assoc($result))
                    {
                        $bookList[] = $row;
                    }
                    return $bookList;
                }
                else
                {
                    return null;
                }    
            }
        }
        
        //Tìm kiếm các cuốn sách trong database
        function searchbooks($searchtype, $keyword){
            if($searchtype == "all")
            {
                $cmd = "select * from books,categories where books.CateId = categories.CateId and (Name like '%" . $keyword . "%' or Author like '%" . $keyword . "%' or Publisher like '%". $keyword . "%')";
            }
            else if($searchtype == "publisher")
            {
                $cmd = "select * from books,categories where books.CateId = categories.CateId and Publisher like '%" . $keyword . "%'";
            }
            else if($searchtype == "author")
            {
                $cmd = "select * from books,categories where books.CateId = categories.CateId and Author like '%" . $keyword . "%'";
            }
            else if($searchtype == "book")
            {
                $cmd = "select * from books,categories where books.CateId = categories.CateId and Name like '%" . $keyword . "%'";
            }
            
            $result = mysql_query($cmd);
            if($result != false)
            {
                if(mysql_num_rows($result) > 0)
                {
                    while($row = mysql_fetch_assoc($result))
                    {
                        $bookList[] = $row;
                    }
                    return $bookList;
                }
                else
                {
                    return null;
                }    
            }
        }
        
        function insertBook($book)
        {
            $code = $book["bookcode"];
            if($this->checkBookCode($code))
            {
                return "Mã sách này đã tồn tại";
            }
            else
            {
                if(isset($book["specialGift"]))
                {
                    $specialGift = "1";
                }
                else
                {
                    $specialGift = "0";
                }
                
                if(isset($book["featuredBook"]))
                {
                    $featuredBook = "1";
                }
                else
                {
                    $featuredBook = "0";
                }
                
                if(empty($book["saleOffPrice"]))
                {
                    $saleOffPrice = 0;
                }
                else
                {
                    $saleOffPrice = $book["saleOffPrice"];
                }
                
                $name = $book["bookname"];
                $detail = $book["detail"];
                $description = $book["description"];
                $image = $book["image"];
                $publisher = $book["publisher"];
                $price = $book["price"];
                
                $status = $book["status"];
                $cateId = $book["cateId"];
                $author = $book["author"];
                
                $cmd = "insert into books (Name,Code,Detail,Description,ImageUrl,Publisher,Price,SaleOffPrice,FeaturedBook,Status,SpecialGift,CateId,Author) values ('" . 
                                            $name . "', '" . $code . "', '" . $detail . "', '" . $description . "', '" . $image . "', '" . $publisher . "', '" . $price . "', '" . 
                                            $saleOffPrice . "'," . $featuredBook . ", '" . $status . "'," . $specialGift . "," . $cateId . ", '" . $author . "')";
                                            
                if (mysql_query($cmd))
                {
                    return "";
                }
                else
                {
                    return mysql_error();
                };
            }
            
        }
        
        function deleteBook($bookId)
        {
            $cmd = "delete from books where BookId = " . $bookId;
            return mysql_query($cmd);
        }
        
        function deleteBooks($bookId_array)
        {
            $count = count($bookId_array);
            foreach($bookId_array as $bookId)
            {
                if(!$this->deleteBook($bookId))
                {
                    $count--;
                };                    
            }  
            return $count;                     
        } 
        
        function getBook($bookId)
        {
            $cmd = "select * from books where BookId = '" . $bookId . "'";
            $result = mysql_query($cmd);
            if($result != false)
            {
                return mysql_fetch_assoc($result);
            }
        }
        
        function updateBook($book)
        {
            $bookcode = $book["bookcode"];
            $pre_bookcode = $book["pre_bookcode"];
            if($this->checkBookCode($bookcode)&& ($pre_bookcode != $bookcode))
            {
                return "Mã sách này đã tồn tại";
            }
            else
            {
                $featuredBook = "0";
                $specialGift = "0";
                if(isset($book["featuredBook"]))
                {
                    $featuredBook = "1";
                }
                if(isset($book["specialGift"]))
                {
                    $specialGift = "1";
                }
                $cmd = "update books set Name = '" . $book["bookname"] . "', 
                                    Code = '" . $book["bookcode"] . "', 
                                    Detail = '" . $book["detail"] . "', 
                                    Description = '" . $book["description"] . "', 
                                    ImageUrl = '" . $book["imageUrl"] . "', 
                                    Publisher = '" . $book["publisher"] . "', 
                                    Price = '" . $book["price"] . "', 
                                    SaleOffPrice = '" . $book["saleOffPrice"] . "',
                                    FeaturedBook = " . $featuredBook . ",
                                    Status = '" . $book["status"] . "',
                                    SpecialGift = " . $specialGift . ",
                                    CateId = " . $book["cateId"] . ",
                                    Author = '" . $book["author"] . "'
                                    Where BookId = " . $book["bookId"];
            
                if (mysql_query($cmd))
                {
                    return "";
                }
                else
                {
                    return mysql_error();
                }
            }                                                                        
        }
        
        
        
        function get_books_by_cate($cid){
            $sql ="select b.BookId,b.Name,b.ImageUrl,c.CateName from books as b, categories as c where b.CateId = c.CateId and b.CateId = ".$cid;
            $arr_return = array(
                'books' => array(),
                'error_message' =>''
            );
            $result = mysql_query($sql);
            if($result!=false){
                $rows_count = mysql_num_rows($result);
                if($rows_count>0){
                    while($row = mysql_fetch_assoc($result)){
                        $arr_return['books'][] = $row;
                    }
                }else{
                    $arr_return['error_message'] = "Không có sách nào thuộc danh mục sách này";
                }
            }else{
                $arr_return['error_message'] = "Có một lỗi xảy ra trong quá trình tải sách";
            }
            
            return $arr_return;
        }
        
        function getAllCategories(){
            $sql = "select * from categories";
            $result = mysql_query($sql);
            if($result != false)
            {
                 if(mysql_num_rows($result) > 0)
                {
                    while($row = mysql_fetch_assoc($result))
                    {
                        $bookList[] = $row;
                    }
                    return $bookList;
                }
                else
                {
                    return null;
                }    
            }
        }
        
    } 

?>